package wowrl.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import wowrl.data.WOWRL_Data;
import wowrl.engine.WOWRL_Engine;
import wowrl.tools.ResourceLoader;

@SuppressWarnings("serial")
public class WOWRL_GUI extends JPanel {
	//Store references to the engine and main JFrame window
	private WOWRL_Engine engine;
	
	//Store the background color
	static Color backgroundColor;
	
	//Store the resource loader
	private ResourceLoader	loader;
	
	//Store the background image that we want to draw.
	private Image 				tileGraphics;
	
	//Create our controls
	private JLabel testBox;
	private BoardPanel boardPanel;
	private InventoryPanel inventoryPanel;
	private StatusPanel statusPanel;
		
	public WOWRL_GUI(WOWRL_Engine engine)
	{
		// Specify the engine and JFrame window
		this.engine = engine;
		
		//Load all graphical resources.
		loader = new ResourceLoader();
		loader.loadResources("wowrl");
						
		// Load the tile graphics
		tileGraphics = loader.getResource("TileSet");
		
		// We will manually layout
		setLayout(null);

		// Set the window size
		setPreferredSize(new Dimension(800, 600)); //160 x 120

		// Specify the background color
		//backgroundColor = new Color((float) 0.3, (float) 0.65, (float) 1);
		
		// Set the window's background color
		//setBackground(backgroundColor);
	
		// Create the board
		boardPanel = new BoardPanel(loader,engine);
		add(boardPanel);
		boardPanel.setLocation(0, 0);
		
		// Create the inventoryPanel
		inventoryPanel = new InventoryPanel(engine);
		add(inventoryPanel);
		inventoryPanel.setLocation(640,0);
		
		// Create the statusPanel
		statusPanel = new StatusPanel(engine);
		add(statusPanel);
		statusPanel.setLocation(0,480);				

		// Set up keyboard input
		createListener();
		
		// Initialize the testBox JLabel
		testBox = new JLabel("INIT ERROR", JLabel.CENTER);
		testBox.setFont(new Font("Serif", Font.BOLD, 18));
		testBox.setForeground(Color.red);
		testBox.setBackground(Color.BLACK);
		testBox.setOpaque(true);
		add(testBox);
		
		// Lay out the controls (X-loc, Y-loc, X-size, Y-size)
		testBox.setBounds    (190,   5, 65,  25);
	}
	
	public void Start()
	{
		//engine.addMessage("Your strength is: " + engine.getPlayer().attributes[0]);
		refreshDisplay();
	}
	
	/*** KEY ACTIONS ***/

	Action actionKeyEscape = new AbstractAction() {
	    public void actionPerformed(ActionEvent e) {
	    	engine.quit();
	    }
	};
	
	Action actionKeyRight = new AbstractAction() {
	    public void actionPerformed(ActionEvent e) {
	    	engine.movePlayer(+1,0);
	    	refreshDisplay();
	    }
	};
	
	Action actionKeyLeft = new AbstractAction() {
	    public void actionPerformed(ActionEvent e) {
	    	engine.movePlayer(-1,0);
	    	refreshDisplay();
	    }
	};

	Action actionKeyDown = new AbstractAction() {
	    public void actionPerformed(ActionEvent e) {
	    	engine.movePlayer(0,+1);
	    	refreshDisplay();
	    }
	};
	

	Action actionKeyUp = new AbstractAction() {
		public void actionPerformed(ActionEvent e) {
	    	engine.movePlayer(0,-1);
	    	refreshDisplay();
	    }
	};
	
	private void createListener(){
		//Escape key

		getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "pressedEscape");
		getActionMap().put("pressedEscape", actionKeyEscape);
		
		//Arrow keys
		getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "pressedUp");
		getActionMap().put("pressedUp", actionKeyUp);
		getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "pressedDown");
		getActionMap().put("pressedDown", actionKeyDown);
		getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "pressedLeft");
		getActionMap().put("pressedLeft", actionKeyLeft);
		getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "pressedRight");
		getActionMap().put("pressedRight", actionKeyRight);
	}

	private void refreshDisplay()
	{
		//testBox: Update location
		//testBox.setLocation(new Point(
		//		engine.getPlayerLocation().x * 32,
		//		engine.getPlayerLocation().y * 32));
		
		//testBox: Set caption
		testBox.setText(engine.getPlayerLocation().x + ":" + engine.getPlayerLocation().y);
		
		setBackground(backgroundColor);
		
		//Repaint the GUI.	
		repaint();
		boardPanel.repaint();
		inventoryPanel.refresh();
		statusPanel.refresh();
	}
}
